/*
 * Created on2022/6/11 15:54
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.yzbdl.lanius.core.plugins.metastore;

import java.sql.SQLException;
import java.util.List;

import org.yzbdl.lanius.core.exception.LaniusEngineDbException;
import org.yzbdl.lanius.core.metastore.HutoolDb;

import cn.hutool.db.Entity;
import cn.hutool.db.handler.BeanHandler;

/**
 * TODO
 *
 * @author hs@yzbdl.ac.cn
 * @version 1.0
 * @date 2019/6/11 15:54
 */
public class LdrPluginCategoryDao {

    private static LdrPluginCategoryDao ldrPluginCategoryDao = new LdrPluginCategoryDao();

    private LdrPluginCategoryDao() {}

    public static LdrPluginCategoryDao getInstance() {
        return ldrPluginCategoryDao;
    }

    /**
     * 添加或修改
     *
     * @param obj
     * @return
     * @throws LaniusEngineDbException
     */
    public synchronized LdrPluginCategoryEntity save(LdrPluginCategoryEntity obj) throws LaniusEngineDbException {
        try {
            Entity entity = Entity.create("ldr_plugin_category").set("category_id", obj.getCategoryId())
                .set("plugin_id", obj.getPluginId()).set("create_time", obj.getCreateTime());
            Long id = HutoolDb.useDynamicUrlDb().insertForGeneratedKey(entity);
            obj.setId(id.intValue());
            return obj;
        } catch (SQLException e) {
            throw new LaniusEngineDbException(e);
        }
    }

    /**
     * 删除
     *
     * @param id
     */
    public synchronized void delete(Integer id) throws LaniusEngineDbException {
        try {
            HutoolDb.useDynamicUrlDb().del("ldr_plugin_category", "id", id);
        } catch (SQLException e) {
            throw new LaniusEngineDbException(e);
        }
    }

    public void delete(Integer pluginId, Integer categoryId) throws LaniusEngineDbException {
        try {
            HutoolDb.useDynamicUrlDb()
                .del(new Entity("ldr_plugin_category").set("plugin_id", pluginId).set("category_id", categoryId));
        } catch (SQLException e) {
            throw new LaniusEngineDbException(e);
        }
    }

    public LdrPluginCategoryEntity getOne(Integer pluginId, int categoryId) throws LaniusEngineDbException {
        try {
            return HutoolDb.useDynamicUrlDb().query(
                "select * from ldr_plugin_category where plugin_id = ? and category_id = ?",
                new BeanHandler<LdrPluginCategoryEntity>(LdrPluginCategoryEntity.class), pluginId, categoryId);
        } catch (SQLException e) {
            throw new LaniusEngineDbException(e);
        }
    }

    public List<LdrPluginCategoryEntity> getAll(int categoryId) throws LaniusEngineDbException {
        try {
            List<LdrPluginCategoryEntity> entityList = HutoolDb.useDynamicUrlDb().query(
                "select * from ldr_plugin_category where category_id = ?", LdrPluginCategoryEntity.class, categoryId);
            return entityList;
        } catch (SQLException e) {
            throw new LaniusEngineDbException(e);
        }
    }

    // /**
    // * 删除无效的关联关系
    // */
    // public synchronized void deleteIneffectiveLink() throws LaniusEngineDbException {
    // try {
    // String sql =
    // "delete from ldr_plugin_category where category_id not in (select id from ldr_category) or plugin_id not in
    // (select id
    // from ldr_plugin)";
    // HutoolDb.useDynamicUrlDb().execute(sql);
    // } catch (SQLException e) {
    // throw new LaniusEngineDbException(e);
    // }
    // }
}